import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSItemTile;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class DrugRaid extends Script implements ServerMessageListener,
		PaintListener {
	public long startTime = System.currentTimeMillis();
	public int druid = 181;

	public RSNPC target = null;

	public int marrentillID = 201;
	public int harralanderID = 205;
	public int ranarrID = 207;
	public int tarrominID = 203;
	public int guamID = 199;
	public int lantadymeID = 2485;
	public int iritID = 209;
	public int cadantineID = 215;
	public int dwarfWeedID = 217;
	public int kwuarmID = 213;
	public int avantoeID = 211;
	public int law = 563;
	public int nature = 561;
	public int ammoID = 13280;// Broad-tipped.

	public int totalMarrentill = 0;
	public int totalHarralander = 0;
	public int totalRanarr = 0;
	public int totalTarromin = 0;
	public int totalGuam = 0;
	public int totalLantadyme = 0;
	public int totalIrit = 0;
	public int totalCadantine = 0;
	public int totalDwarf = 0;
	public int totalKwuarm = 0;
	public int totalAvantoe = 0;
	public int totalLaw = 0;
	public int totalNat = 0;

	public int bones = 526;

	public int eatOn = 10;
	public boolean eatFood;
	public int foodID = 385;
	public boolean agility;

	public boolean looting = false;

	public boolean inDoor = false;
	public boolean atBank = false;

	public int log1 = 35999;
	public int log2 = 35997;

	static final int[] bankWindows = new int[] { 34752 };

	public RSItemTile bone = getGroundItemByID(5, bones);

	public final RSTile[] bankpath = new RSTile[] { new RSTile(2565, 3356),
			new RSTile(2576, 3350), new RSTile(2584, 3350),
			new RSTile(2581, 3363), new RSTile(2590, 3372),
			new RSTile(2604, 3368), new RSTile(2612, 3363),
			new RSTile(2614, 3351), new RSTile(2612, 3340),
			new RSTile(2617, 3333) };

	public final RSTile[] bankpathagil = new RSTile[] { new RSTile(2565, 3356),
			new RSTile(2576, 3350), new RSTile(2585, 3342),
			new RSTile(2589, 3332), new RSTile(2598, 3336) };

	public final RSTile[] afterlog1 = new RSTile[] { new RSTile(2602, 3336),
			new RSTile(2612, 3339), new RSTile(2616, 3332) };

	public final RSTile[] returnpath = reversePath(bankpath);

	public final RSTile[] returnlog1 = reversePath(afterlog1);
	public final RSTile[] returnbankagil1 = reversePath(bankpathagil);

	public boolean gotOverLog = false;

	public int deathAnim = 836;

	public void bank() {
		log("banking");
		final RSObject bankWindow = findObject(DrugRaid.bankWindows);
		final RSTile bankspot = new RSTile(2565, 3356);
		if (getMyPlayer().getLocation().getX() < 2565) {
			walkTileMM(bankspot);
			if (getObjectAt(2564, 3356) == null) {
				wait(random(500, 1000));
			} else {
				setCompass('e');
				atObject(getObjectAt(2564, 3356), "Open");
			}
		} else if (getMyPlayer().getLocation().getX() == 2565
				&& getMyPlayer().getLocation().getY() == 3356) {
			inDoor = false;
			log("We got out... Fucken awesome!");
			walkTileMM(bankpathagil[findBestPathTile(bankpathagil)]);
		} else if (agility) {
			if (!gotOverLog) {
				if (!getMyPlayer().isMoving()
						&& getMyPlayer().getLocation().getX() <= 2598
						&& getMyPlayer().getLocation().getX() >= 2596) {
					log("At log");
					walkOverlog(log2);
				} else if (getMyPlayer().getLocation().getX() < 2598
						&& !getMyPlayer().isMoving()) {
					log("Not at log yet.");
					walkTileMM(bankpathagil[findBestPathTile(bankpathagil)]);
					wait(random(800, 1500));
				}
			}
			if (gotOverLog) {
				log("in first movement");
				if (getMyPlayer().getLocation().getX() >= 2602
						&& !getMyPlayer().isMoving()) {
					log("In movement");
					walkTileMM(afterlog1[findBestPathTile(afterlog1)]);
					wait(random(800, 1500));
				}
			}
		} else if (agility == false) {
			log("Trying to bank");
			walkTileMM(returnpath[findBestPathTile(returnpath)]);
			wait(random(800, 1500));
		}
		if (bankWindow != null && gotOverLog == true
				&& getMyPlayer().getLocation().getX() >= 2614) {
			if (distanceTo(bankWindow.getLocation()) > 5
					&& !getMyPlayer().isMoving()) {
				walkTileMM(bankWindow.getLocation());
			} else {
				log("At bank");
				atBank = true;
			}
		}
		if (!RSInterface.getInterface(762).isValid() && atBank == true) {
			atObject(bankWindow, "Use-quickly");
			wait(random(2000, 3500));
			bank.depositAll();
			if (eatFood) {
				clickMouse(55, 107, 10, 10, true);
				wait(random(20, 1200));
				clickMouse(55, 107, 10, 10, true);
				wait(random(20, 1200));
				clickMouse(55, 107, 10, 10, true);
				wait(random(20, 1200));
				clickMouse(55, 107, 10, 10, true);
				wait(random(20, 1200));
			}
		}
	}

	public void checkFood() {
		if (skills.getCurrentSkillLevel(3) <= eatOn) {
			log("We have to eat.");
			clickInventoryItem(foodID, true);
			wait(random(1000, 2000));
		} else {
			log("We don't need to eat... Fucken awesome");
		}
	}

	public boolean clickInventoryItem(final int itemID, final boolean click) {
		if (getCurrentTab() != Constants.TAB_INVENTORY
				&& !RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()
				&& !RSInterface.getInterface(Constants.INTERFACE_STORE)
						.isValid()) {
			openTab(Constants.TAB_INVENTORY);
		}
		final int[] items = getInventoryArray();
		final java.util.List<Integer> possible = new ArrayList<Integer>();
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				possible.add(i);
			}
		}
		if (possible.size() == 0) {
			return false;
		}
		final int idx = possible.get(random(0, possible.size()));
		final Point t = getInventoryItemPoint(idx);
		clickMouse(t, 5, 5, click);
		return true;
	}

	public void fight() {
		if (eatFood) {
			checkFood();
		}
		target = getNearestFreeNPCByID(druid);
		final RSItemTile bone = getGroundItemByID(bones);
		if (!isInventoryFull()) {
			if (!getMyPlayer().isInCombat() && target != null) {
				atNPC(target, "Attack");
				wait(random(500, 2000));
			} else {
				log("In combat or no druids");
			}
			if (bone != null) {
				log("Looting time");
				loot();
			}
		}
	}

	public int findBestPathTile(final RSTile[] path) {
		int value = 0;
		for (int i = 0; i < path.length; i++) {
			if (distanceTo(path[i]) <= 16) {
				value = i;
			}
		}
		return value;
	}

	@Override
	public String getAuthor() {
		return "Lowrie";
	}

	@Override
	public String getName() {
		return "Drug Raid";
	}

	@Override
	public String getScriptCategory() {
		return "Herblore";
	}

	@Override
	public String getScriptDescription() {
		String html = "";

		html += "<html>\n";
		html += "<body>\n";
		html += "<h2>" + getName() + " v" + getVersion() + "</h2><br>\n";
		html += "<b>Author: </b>" + getAuthor() + "<br>\n";
		html += "Start the either at the East Ardougne bank or in with the druids.";
		html += "<br>If you chose to eat put food in the first spot in bank.";
		html += "<br><br><b>Eat food?:</b><input type=\"checkbox\" name=\"food\" value=\"true\">";
		html += "<br><b>Enter the food ID</b><input type=\"text\" name=\"foodID\" value=\"385\" size = \"4\">";
		html += "<br><b>Enter Ammo ID</b><input type=\"text\" name=\"ammoID\" value=\"13280\" size = \"5\">";
		html += "<br><br><b>Use Agility Route:</b><input type=\"checkbox\" name=\"agil\" value=\"true\" checked=\"true\">";
		html += "</body>\n";
		html += "</html\n";

		return html;
	}

	@Override
	public double getVersion() {
		return 0.03;
	}

	@Override
	public int loop() {
		setRun(true);
		if (!isInventoryFull()) {
			if (getMyPlayer().getLocation().getX() < 2565 && inDoor == true) {
				fight();
			} else {
				returnFromBank();
			}
		} else {
			gotOverLog = false;
			bank();
		}
		return 1;
	}

	public void loot() {
		final RSItemTile marrentill = getGroundItemByID(marrentillID);
		final RSItemTile harralander = getGroundItemByID(harralanderID);
		final RSItemTile ranarr = getGroundItemByID(ranarrID);
		final RSItemTile tarromin = getGroundItemByID(tarrominID);
		final RSItemTile guam = getGroundItemByID(guamID);
		final RSItemTile lantadyme = getGroundItemByID(lantadymeID);
		final RSItemTile irit = getGroundItemByID(iritID);
		final RSItemTile cadantine = getGroundItemByID(cadantineID);
		final RSItemTile dwarfWeed = getGroundItemByID(dwarfWeedID);
		final RSItemTile kwuarm = getGroundItemByID(kwuarmID);
		final RSItemTile avantoe = getGroundItemByID(avantoeID);
		final RSItemTile lawz = getGroundItemByID(law);
		final RSItemTile nat = getGroundItemByID(nature);
		final RSItemTile ammo = getGroundItemByID(ammoID);
		if (!getMyPlayer().isInCombat()) {
			if (atTile(ammo, "bolt") || atTile(ammo, "arrow")) {
				log("Picked up stray ammo");
				wait(random(800, 1500));
				clickInventoryItem(ammoID, true);
			}
			if (atTile(lawz, "law")) {
				log("We got a law rune.");
				totalLaw += 2;
				wait(random(500, 1000));
			} else if (atTile(nat, "nat")) {
				log("We got the nat.");
				totalNat += 2;
				wait(random(500, 1000));
			} else if (guam != null) {
				atTile(guam, "guam");
				totalGuam += 1;
				log("Got the guam");
			} else if (marrentill != null) {
				atTile(marrentill, "marrentill");
				totalMarrentill += 1;
				log("Got the marrentill");
			} else if (harralander != null) {
				atTile(harralander, "harralander");
				totalHarralander += 1;
				log("Got the harralander");
			} else if (ranarr != null) {
				atTile(ranarr, "ranarr");
				totalRanarr += 1;
				log("Got the ranarr, w00t monehz");
			} else if (tarromin != null) {
				atTile(tarromin, "tarromin");
				totalTarromin += 1;
				log("Got the tarromin");
			} else if (lantadyme != null) {
				atTile(lantadyme, "lantadyme");
				totalLantadyme += 1;
				log("Got the lantadyme");
			} else if (irit != null) {
				atTile(irit, "irit");
				totalIrit += 1;
				log("Got the irit");
			} else if (cadantine != null) {
				atTile(cadantine, "cadantine");
				totalCadantine += 1;
				log("Got the cadantine");
			} else if (dwarfWeed != null) {
				atTile(dwarfWeed, "dwarf weed");
				totalDwarf += 1;
				log("Got some weed");
			} else if (kwuarm != null) {
				atTile(kwuarm, "kwuarm");
				totalKwuarm += 1;
				log("Got the kwuarm");
			} else if (avantoe != null) {
				atTile(avantoe, "avantoe");
				totalAvantoe += 1;
				log("Got the avantoe");
			}
		}
	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;
			final int x = 12;
			int y = 136;
			final int marrentillPrice = 175;
			final int harralanderPrice = 734;
			final int ranarrPrice = 6204;
			final int tarrominPrice = 283;
			final int guamPrice = 462;
			final int lantadymePrice = 1124;
			final int iritPrice = 1186;
			final int cadantinePrice = 1081;
			final int dwarfPrice = 1896;
			final int kwuarmPrice = 2733;
			final int avantoePrice = 1425;
			final int lawPrice = 293;
			final int natPrice = 222;

			final int profit = marrentillPrice * totalMarrentill
					+ harralanderPrice * totalHarralander + ranarrPrice
					* totalRanarr + tarrominPrice * totalTarromin + guamPrice
					* totalGuam + lantadymePrice * totalLantadyme + iritPrice
					* totalIrit + cadantinePrice * totalCadantine + dwarfPrice
					* totalDwarf + kwuarmPrice * totalKwuarm + avantoePrice
					* totalAvantoe + lawPrice * totalLaw + natPrice * totalNat;
			g.setColor(new Color(255, 0, 0, 50));
			g.fillRoundRect(3, 120, 274, 46, 5, 5);
			g.setColor(Color.white);
			g.drawString("Drug raid - Lowrie", x, y);
			g.drawString("Estimated profit: " + profit + ".", x, y += 12);
			g.drawString("Time Running : " + hours + " hours " + minutes
					+ " minutes " + seconds + " seconds.", x, y += 12);
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		startTime = System.currentTimeMillis();
		try {
			foodID = Integer.parseInt(args.get("foodID"));
			ammoID = Integer.parseInt(args.get("ammoID"));
		} catch (final Exception e) {

		}
		if (args.get("agil") != null) {
			if (args.get("agil").equals("true")) {
				agility = true;
			}
		}
		if (args.get("food") != null) {
			if (args.get("food").equals("true")) {
				eatFood = true;
			}
		}
		return true;
	}

	void pickdoor() {
		if (getObjectAt(2564, 3356) == null) {
			wait(random(500, 1000));
		} else {
			setCompass('e');
			atObject(getObjectAt(2564, 3356), "Pick");
		}
	}

	public void returnFromBank() {
		atBank = false;
		if (getMyPlayer().getLocation().getX() == 2565
				&& getMyPlayer().getLocation().getY() == 3356) {
			pickdoor();
		} else if (agility) {
			if (!gotOverLog) {
				if (getMyPlayer().getLocation().getX() > 2602
						&& !getMyPlayer().isMoving()) {
					log("Not at log yet.");
					walkTileMM(returnlog1[findBestPathTile(returnlog1)]);
					wait(random(800, 1500));
				} else if (!getMyPlayer().isMoving()
						&& getMyPlayer().getLocation().getX() <= 2605
						&& getMyPlayer().getLocation().getX() >= 2602) {
					log("At log");
					walkOverlog(log1);
				}
			}
			if (gotOverLog) {
				log("in first movement");
				if (getMyPlayer().getLocation().getX() <= 2602
						&& !getMyPlayer().isMoving()) {
					log("In movement");
					walkTileMM(returnbankagil1[findBestPathTile(returnbankagil1)]);
					wait(random(800, 1500));
				}
			}
		} else if (agility == false) {
			if (!getMyPlayer().isMoving()) {
				log("Walking back");
				walkTileMM(bankpath[findBestPathTile(bankpath)]);
				wait(random(800, 1500));
			}
		}
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		final String message = e.getMessage().toLowerCase();
		if (message.contains("log")) {
			log("Made it over the log");
			gotOverLog = true;
		}
		if (message.contains("you manage to pick the lock")) {
			log("We got in the door");
			inDoor = true;
			gotOverLog = false;
			fight();
		}
	}

	public void walkHalfMM(final RSNPC c) {
		final RSTile t = c.getLocation();
		walkTileMM(t);
	}

	void walkOverlog(final int input) {
		final RSObject log = findObject(input);
		if (log == null) {
			log("object is null");
			wait(random(500, 1000));
		}
		if (atObject(log, "Walk-across")) {
			log("Should be crossing");
		}
	}
}